﻿using FantasyDraft2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FantasyDraft2.Controllers
{
    public class PlayerHistoryController : Controller
    {

        FantasyDraftEntities fantasyDB = new FantasyDraftEntities();
        //
        // GET: /PlayerHistory/

        public ActionResult Index()
        {
            PlayerHistoryViewModel model = new PlayerHistoryViewModel
            {
                players = fantasyDB.Players.ToList(),
                playerId = 0
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(PlayerHistoryViewModel model)
        {
            SinglePlayerViewModel newModel = new SinglePlayerViewModel
            {
                events = fantasyDB.PlayerEvents.Where(x => x.PlayerId == model.playerId),
                playername = fantasyDB.Players.SingleOrDefault(x => x.PlayerId == model.playerId).Name,
                comments = fantasyDB.PlayerComments.Where(x => x.PlayerId == model.playerId),
                playerId = model.playerId
            };

            TempData["pi"] = newModel;
            return RedirectToAction("ViewHistory");
        }

        public ActionResult ViewHistory()
        {
            SinglePlayerViewModel model = (SinglePlayerViewModel)TempData["pi"];

            Dictionary<int, String> dictionary = new Dictionary<int, string>();

            IEnumerable<User> users = fantasyDB.Users.ToList();
            foreach (User u in users)
            {
                dictionary.Add(u.TeamId, u.TeamName);
            }

            dictionary.Add(0, "None");

            model.teamnames = dictionary;

            return View(model);
        }

        [HttpPost]
        public ActionResult ViewHistory(SinglePlayerViewModel model, String button)
        {
            switch (button)
            {
                case "Add Comment":
                    AddCommentViewModel newModel = new AddCommentViewModel
                    {
                        playerId = model.playerId
                    };
                    TempData["pd"] = newModel;
                    return RedirectToAction("AddComment");
                case "Go Back":
                    return RedirectToAction("Index");
            }
            return RedirectToAction("Index");

        }

        public ActionResult AddComment()
        {
            AddCommentViewModel model = (AddCommentViewModel)TempData["pd"];
            return View(model);
        }

        [HttpPost]
        public ActionResult AddComment(AddCommentViewModel model)
        {
            PlayerComment comment = new PlayerComment
            {
                Comment = model.comment,
                PlayerId = model.playerId,
                Player = fantasyDB.Players.SingleOrDefault(x => x.PlayerId == model.playerId)
            };

            fantasyDB.PlayerComments.Add(comment);
            fantasyDB.SaveChanges();

            SinglePlayerViewModel temp = new SinglePlayerViewModel
            {
                events = fantasyDB.PlayerEvents.Where(x => x.PlayerId == model.playerId),
                playername = fantasyDB.Players.SingleOrDefault(x => x.PlayerId == model.playerId).Name,
                comments = fantasyDB.PlayerComments.Where(x => x.PlayerId == model.playerId),
                playerId = model.playerId
            };
            TempData["pi"] = temp;

            return RedirectToAction("ViewHistory");
        }

    }
}
